﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataObject.Database;

public partial class Pages_Reports_AccPayableReport : System.Web.UI.Page
{
    const string url = @"/Pages/Reports/AccPayableReport.aspx?ProviderName={0}&BoatID={1}&ClientType={2}&FromDate={3:d}&ToDate={4:d}&SortField={5}&SortOption={6}";

    #region Fields & Properties
    private DateTime? _fromDate, _toDate;
    private string _providerName, _boatID;
    private int _clientType;

    private int PageIndex
    {
        get
        {
            if (Request["page"] != null)
            {
                return int.Parse(Request["page"].ToString());
            }
            return 1;
        }
    }
    #endregion

    #region Methods

    /// <summary>
    /// Load Boats
    /// </summary>
    private void LoadBoats()
    {
        // Load Boats
        var boatList = DataObject.DataAccess.BoatDAO.CreateInstant().GetAllBoats();
        boatList.Insert(0, new BoatEntity() { BoatName = "All boats", BoatID = String.Empty });

        this.ddlBoat.DataSource = boatList;
        this.ddlBoat.DataTextField = "BoatName";
        this.ddlBoat.DataValueField = "BoatID";
        this.ddlBoat.DataBind();
    }

    /// <summary>
    /// 
    /// </summary>
    private void GetQueryString()
    {
        // ClientType
        _clientType = -1;
        if (Request["ClientType"] != null) Int32.TryParse(Request["ClientType"], out _clientType);
        ddlClientType.SelectedValue = _clientType.ToString();

        this._providerName = (Request["ProviderName"] != null ? Request["ProviderName"] : String.Empty);
        this.txtProviderName.Text = _providerName;

        this._boatID = (Request["BoatID"] != null ? Request["BoatID"] : String.Empty);
        this.ddlBoat.SelectedValue = _boatID;

        DateTime fromDate, toDate;

        // FromDate
        if (DateTime.TryParse(Request["FromDate"], System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out fromDate))
            _fromDate = fromDate.Date;
        else
            _fromDate = DateTime.Today;

        txtFromDate.Text = _fromDate.Value.ToShortDate();

        // ToDate
        // FromDate
        if (DateTime.TryParse(Request["ToDate"], System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out toDate))
            _toDate = toDate.Date;
        else
            _toDate = DateTime.Today;

        txtToDate.Text = _toDate.Value.ToShortDate();
    }

    /// <summary>
    /// 
    /// </summary>
    private void GetOnForm()
    {
        DateTime fromDate, toDate;
        // FromDate
        if (DateTime.TryParse(txtFromDate.Text, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out fromDate))
            _fromDate = fromDate.Date;
        else
            _fromDate = DateTime.Now;

        // ToDate
        if (DateTime.TryParse(txtToDate.Text, System.Globalization.CultureInfo.InvariantCulture, System.Globalization.DateTimeStyles.None, out toDate))
            _toDate = toDate.Date;
        else
            _toDate = DateTime.Now;

        // Client Type
        this._clientType = Int32.Parse(this.ddlClientType.SelectedValue);
        this._boatID = this.ddlBoat.SelectedValue;
        this._providerName = this.txtProviderName.Text;
    }

    private void BuildOnGrid(out int count)
    {
        HashSet<string> hs = new HashSet<string>();
        hs.Add("ProviderName ASC");
        hs.Add("ServiceName ASC");
        hs.Add("Date ASC");

        grv.DataSource = DataObject.DataAccess.ReservationDAO.CreateInstant().GetViewAccountPayableDetails(_fromDate, _toDate, _clientType, _boatID, _providerName, PageIndex, define.PageSize, out count, hs);
        grv.DataBind();
    }

    /// <summary>
    /// Search method
    /// </summary>
    private void Search()
    {
        int count = 0;
        BuildOnGrid(out count);

        smartPager.PageSize = define.PageSize;
        smartPager.QueryStringKey = "page";
        smartPager.RowCount = count;
        smartPager.UrlFormatString = String.Concat(String.Format(url, _providerName, _boatID, _clientType, _fromDate, _toDate), "&page={0}");
        smartPager.Visible = count > define.PageSize;
    }

    /// <summary>
    /// Print method
    /// </summary>
    private void Print()
    {
        int count = 0;
        BuildOnGrid(out count);

        var form = new System.Web.UI.HtmlControls.HtmlForm();
        form.Controls.Add(grv); // gvParent là gridview 

        string attachment = String.Format("attachment; filename=accountant{0:yyyyMMdd}.xls", DateTime.Now);
        Response.ClearContent();
        Response.AddHeader("content-disposition", attachment);
        Response.ContentType = "application/ms-excel";
        Response.Charset = String.Empty;
        Response.ContentEncoding = System.Text.Encoding.Unicode;
        Response.BinaryWrite(System.Text.Encoding.Unicode.GetPreamble());

        System.IO.StringWriter stw = new System.IO.StringWriter();
        HtmlTextWriter htextw = new HtmlTextWriter(stw);
        this.Controls.Add(form);
        form.RenderControl(htextw);

        Response.Write(stw.ToString());
        Response.End();
    }
    #endregion

    #region Events

    /// <summary>
    /// Page Load
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.LoadBoats();

            // Process Searching
            this.GetQueryString();
            this.Search();
        }
    }

    /// <summary>
    /// Button Find
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        GetOnForm();
        Response.Redirect(String.Format(url, _providerName, _boatID, _clientType, _fromDate, _toDate));
    }

    /// <summary>
    /// Button Print
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnReport_Click(object sender, EventArgs e)
    {
        this.GetQueryString();
        this.Print();
    }
    #endregion

}